//package com.geostar.constructionland.zuul.filter;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.geostar.constructionland.zuul.util.aesrsa.AES;
//import com.geostar.constructionland.zuul.util.aesrsa.ConvertUtils;
//import com.geostar.constructionland.zuul.util.aesrsa.EncryUtil;
//import com.geostar.constructionland.zuul.util.aesrsa.RSA;
//import com.netflix.zuul.ZuulFilter;
//import com.netflix.zuul.context.RequestContext;
//import com.netflix.zuul.exception.ZuulException;
//import com.netflix.zuul.http.ServletInputStreamWrapper;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.http.HttpMethod;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StreamUtils;
//
//import javax.servlet.ServletInputStream;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletRequestWrapper;
//import java.io.IOException;
//import java.io.InputStream;
//import java.nio.charset.Charset;
//
///**
// * 签名
// * @author yelll
// * @since 2020-1-9 11:29
// **/
//@Component
//@Slf4j
//public class SignFilter extends ZuulFilter {
//    @Value("${clientPublicKey}")
//    private String clientPublicKey;
//    @Value("${serverPrivateKey}")
//    private String serverPrivateKey;
//    @Override
//    public String filterType() {
//        return "pre";
//    }
//
//    @Override
//    public int filterOrder() {
//        return 0;
//    }
//
//    @Override
//    public boolean shouldFilter() {
//        /**拦截post请求**/
//        RequestContext ctx = RequestContext.getCurrentContext();
//        HttpServletRequest request = ctx.getRequest();
//        String requstUrl = request.getRequestURL().toString();
//        log.info("访问请求路径:{}",requstUrl);
//        if(requstUrl.contains("list") || requstUrl.contains("query")){
//            log.info("不需要签名验证!");
//            return false;
//        }
//        log.info("访问请求类型为:{}",request.getMethod());
//        if(HttpMethod.GET.toString().equals(request.getMethod())) {
//            return false;
//        }
//        return true;
//    }
//
//    @Override
//    public Object run() throws ZuulException {
//        // 获取到request
//        RequestContext ctx = RequestContext.getCurrentContext();
//        HttpServletRequest request = ctx.getRequest();
//        try{
//            InputStream in = request.getInputStream();
//            String method = request.getMethod();
//            String interfaceMethod = request.getServletPath();
//            log.info("请求方法method={},url={}",method,interfaceMethod);
//            String reqBody = StreamUtils.copyToString(in, Charset.forName("UTF-8"));
//            String conType = request.getHeader("content-type");
//            log.info("数据格式{}",conType);
//            if(reqBody != null && conType.toLowerCase().contains("application/json")){
//                /**对数据进行校验**/
//                JSONObject paramObj = JSON.parseObject(reqBody);
//                String data = paramObj.getString("data");
//                String key = paramObj.getString("key");
//                if(StringUtils.isNotBlank(data) && StringUtils.isNotBlank(key)){
//                    boolean passSign = EncryUtil.checkDecryptAndSign(data,
//                            key, clientPublicKey, serverPrivateKey);
//                    if (passSign) {
//                        /**验签通过**/
//                        String aeskey = RSA.decrypt(key, serverPrivateKey);
//                        String paramJson = ConvertUtils.hexStringToString(AES.decryptFromBase64(data, aeskey));
//                        final byte[] reqBodyBytes = paramJson.getBytes();
//                        /**解析参数，重构上下文**/
//                        ctx.setRequest(new HttpServletRequestWrapper(request) {
//                            @Override
//                            public ServletInputStream getInputStream() throws IOException {
//                                return new ServletInputStreamWrapper(reqBodyBytes);
//                            }
//                            @Override
//                            public int getContentLength() {
//                                return reqBodyBytes.length;
//                            }
//                            @Override
//                            public long getContentLengthLong() {
//                                return reqBodyBytes.length;
//                            }
//                        });
//                    } else {
//                        ctx.setSendZuulResponse(false);  //终止执行
//                        ctx.setResponseBody("警告:令牌无效，无法访问，您的ip已被记录!!");
//                        ctx.setResponseStatusCode(403);
//                        return null;
//                    }
//                }else {
//                    ctx.setSendZuulResponse(false);  //终止执行
//                    ctx.setResponseBody("警告:没有令牌,无法访问!!");
//                    ctx.setResponseStatusCode(403);
//                    return null;
//                }
//            }else {
//                ctx.setSendZuulResponse(false);  //终止执行
//                ctx.setResponseBody("警告:请使用application/json传输数据,post请求传输数据");
//                ctx.setResponseStatusCode(403);
//                return null;
//            }
//
//        }catch (Exception e){
//        }
//        return null;
//    }
//}
//
